// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//

#pragma once

#include <cstdint>
#include <functional>
#include <memory>
#include <vector>

#include <nebula/types/type.h>
#include <nebula/types/type_fwd.h>

namespace nebula::util::internal {

class TURBO_EXPORT NestedListGenerator {
 public:
  /// \brief create a nested FixedSizeListType.
  ///
  /// \return `fixed_size_list(fixed_size_list(..., sizes[1]), sizes[0])`
  static std::shared_ptr<DataType> NestedFSLType(
      const std::shared_ptr<DataType>& inner_type, const std::vector<int>& sizes);

  /// \brief create a nested FixedListType.
  ///
  /// \return `list(list(...))`
  static std::shared_ptr<DataType> NestedListType(
      const std::shared_ptr<DataType>& inner_type, size_t depth);

  static turbo::Result<std::shared_ptr<Array>> NestedFSLArray(
      const std::shared_ptr<DataType>& inner_type, const std::vector<int>& list_sizes,
      int64_t length);

  static turbo::Result<std::shared_ptr<Array>> NestedListArray(
      const std::shared_ptr<DataType>& inner_type, const std::vector<int>& list_sizes,
      int64_t length);

  /// \brief Generate all possible nested list configurations of depth 1 to max_depth.
  ///
  /// Each configuration consists of a single inner value type and a list of sizes.
  /// Both can be used with NestedFSLArray and NestedListArray to generate test data.
  ///
  /// The product of the list sizes and the size of the inner value type is always a power
  /// of 2 no greater than max_power_of_2_size. For max_depth=3 and
  /// max_power_of_2_size=32, this generates 108 configurations.
  static void VisitAllNestedListConfigurations(
      const std::vector<std::shared_ptr<DataType>>& inner_value_types,
      const std::function<void(const std::shared_ptr<DataType>&,
                               const std::vector<int>&)>& visit,
      int max_depth = 3, int max_power_of_2_size = 32);

 private:
  // append([...[[*next_inner_value++, *next_inner_value++, ...]]...])
  static turbo::Status AppendNestedList(ArrayBuilder* nested_builder, const int* list_sizes,
                                 int64_t* next_inner_value);

  static turbo::Result<std::shared_ptr<Array>> NestedListArray(
      ArrayBuilder* nested_builder, const std::vector<int>& list_sizes, int64_t length);
};

}  // namespace nebula::util::internal
